#include "njm_component_mgr.hpp"

namespace njm
{
    ComponentManager::ComponentManager()
    {
        Init();
    }
    ComponentManager::~ComponentManager()
    {
        Clear();
    }
    void ComponentManager::Init()
    {
        mComponentTypes.clear();
        mComponentArrays.clear();
        mNextComponentType = nullComponentType;
    }
    void ComponentManager::Clear()
    {
        mComponentTypes.clear();
        mComponentArrays.clear();
        mNextComponentType = nullComponentType;
    }
    // 
    // template <typename T>
    // void ComponentManager::RegisterComponent()
    // {
    //     const char *typeName = typeid(T).name();
    //     assert(mComponentTypes.find(typeName) == mComponentTypes.end() && "Registering component type more than once.");
    //     // 把新的ComponentType加入映射, 创建新的ComponentArray，并将其加入映射
    //     mComponentTypes.insert({typeName, mNextComponentType});
    //     mComponentArrays.insert({typeName, std::make_shared<ComponentArray<T>>()});
    //     ++mNextComponentType;
    // }

    // template <typename T>
    // ComponentType ComponentManager::GetComponentType()
    // {
    //     const char *typeName = typeid(T).name();
    //     assert(mComponentTypes.find(typeName) != mComponentTypes.end() && "Component not registered before use.");
    //     return mComponentTypes[typeName];
    // }

    // template <typename T>
    // Signature ComponentManager::GetComponentSignature()
    // {
    //     const char *typeName = typeid(T).name();
    //     assert(mComponentTypes.find(typeName) != mComponentTypes.end() && "Component not registered before use.");
    //     ComponentType transformType = mComponentTypes[typeName];
    //     return Signature().set(transformType, true);
    // }

    // template <typename T>
    // const char *ComponentManager::GetComponentName()
    // {
    //     const char *typeName = typeid(T).name();
    //     assert(mComponentTypes.find(typeName) != mComponentTypes.end() && "Component not registered before use.");
    //     return typeid(T).name();
    // }

    // template <typename T>
    // void ComponentManager::AddComponent(Entity entity, T component)
    // {
    //     GetComponentArray<T>()->InsertData(entity, component);
    // }
    // template <typename T>
    // void ComponentManager::RemoveComponent(Entity entity)
    // {
    //     GetComponentArray<T>()->RemoveData(entity);
    // }
    // template <typename T>
    // T &ComponentManager::GetComponent(Entity entity)
    // {
    //     return GetComponentArray<T>()->GetData(entity);
    // }
    // void ComponentManager::EntityDestroyed(Entity entity)
    // {
    //     // 将每个ComponentArray中 该entity对应的Component删掉
    //     for (auto const &pair : mComponentArrays)
    //     {
    //         auto const &pComponentArr = pair.second;
    //         pComponentArr->EntityDestroyed(entity);
    //     }
    // }

    // template <typename T>
    // std::shared_ptr<ComponentArray<T>> ComponentManager::GetComponentArray()
    // {
    //     const char *typeName = typeid(T).name();
    //     assert(mComponentTypes.find(typeName) != mComponentTypes.end() && "Component not registered yet.");
    //     return std::static_pointer_cast<ComponentArray<T>>(mComponentArrays[typeName]);
    // }
}
